import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置请求和响应的编码为 utf-8
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        // 设置响应内容类型为 JSON
        resp.setContentType("application/json");

        // 获取用户名和密码参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        // 判断用户名和密码是否正确
        if (username.equals("rose") && password.equals("123456")) {
            // 登录成功，返回 JSON 格式的成功信息
            PrintWriter writer = resp.getWriter();
            writer.print("{\"code\":1,\"msg\":\"success\"}");
        } else {
            // 登录失败，返回 JSON 格式的失败信息
            PrintWriter writer = resp.getWriter();
            writer.print("{\"code\":0,\"msg\":\"failure\"}");
        }
    }
}